<template>
  <div style="padding: 1rem">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="主键Id">
        <el-input v-model="formInline.id" placeholder="主键Id" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="success" @click="add">新增</el-button>
      </el-form-item>
    </el-form>
    <!--数据表-->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="id"
        label="id"
        width="150"
      />
      <el-table-column
        prop="userId"
        label="用户Id"
      />
      <el-table-column
        prop="method"
        label="操作日志"
      />
      <el-table-column
        prop="createTime"
        label="操作时间"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  分页-->
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[2,10,50,100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑用户':'添加用户'">
      <el-form :model="data" label-width="80px" label-position="left">
        <el-form-item label="用户名字">
          <el-input v-model="data.id" placeholder="用户名字" />
        </el-form-item>
        <el-form-item label="用户名字">
          <el-input v-model="data.userId" placeholder="用户名字" />
        </el-form-item>
        <el-form-item label="用户名字">
          <el-input v-model="data.method" placeholder="用户名字" />
        </el-form-item>
        <el-form-item label="用户名字">
          <el-input v-model="data.createTime" placeholder="用户名字" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmRole">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getLogList, delLog, updLog, addLog } from '@/api/log'
const defaultLog = {
  id: '',
  userId: '',
  method: '',
  createTime: ''
}
export default {
  name: 'Log',
  data() {
    return {
      data: {},
      dialogType: '',
      dialogVisible: false,
      loading: true,
      formInline: {
        id: ''
      },
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1
    }
  },
  created() {
    this.getLogList()
  },
  methods: {
    search() {
      const data = {
        id: this.formInline.id,
        page: this.currentPage,
        pageSize: this.pageSize
      }
      getLogList(data).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
      this.loading = false
    },
    // 获取数据表
    async getLogList() {
      const data = {
        id: this.formInline.id,
        page: this.currentPage,
        pageSize: this.pageSize
      }
      await getLogList(data).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
      this.loading = false
    },
    del(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delLog({ id: row.id }).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getLogList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    edit(row) {
      this.data = row
      this.dialogType = 'edit'
      this.dialogVisible = true
    },
    add() {
      this.data = Object.assign({}, defaultLog)
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    async confirmRole() {
      const isEdit = this.dialogType === 'edit'
      if (isEdit) {
        await updLog(this.data)
        this.$notify({
          title: 'Success',
          dangerouslyUseHTMLString: true,
          message: `编辑成功`,
          type: 'success'
        })
      } else {
        await addLog(this.data)
        this.$notify({
          title: 'Success',
          dangerouslyUseHTMLString: true,
          message: `添加成功`,
          type: 'success'
        })
      }
      this.getLogList()
      this.dialogVisible = false
    },
    // 分页方法
    // 点击下一页
    handleCurrentChange(val) {
      this.currentPage = val
      this.search()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.search()
    }
  }
}
</script>

<style scoped>

</style>
